package com.wdh.dbutil;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;

import java.io.FileOutputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class SqlGenerator {

   private static String url = "jdbc:mysql://localhost:3306/test";
   private static String user = "root";
   private  static String password = "hnbc0658";
   private static final String INTERVAL = " ";

    public static void main(String[] args)throws Exception {
        //todo 修改表名
        String tableName = "t_user";
        List<String> columnList = new ArrayList<>();
        Map<String,String> columnMap = new LinkedHashMap<>();
        try (Connection connection = DriverManager.getConnection(url, user, password)) {
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet columns = metaData.getColumns(null, null, tableName, null);
            while (columns.next()) {
                String columnName = columns.getString("COLUMN_NAME");
                String typeName = columns.getString("TYPE_NAME");
                columnMap.put(columnName,typeName);
                columnList.add(columnName);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        //genInsert(columnList,tableName);
        //genSelectById(columnList,tableName);
        //genUpdate(columnList,tableName);
    }

    private static void genInsert(List<String> columnList,String tableName){
        StringBuilder builder = new StringBuilder("INSERT INTO ").append(tableName);
        builder.append("(");
        int size = columnList.size();
        int index = 0;
        for (String str : columnList) {
            builder.append("\n\r").append(INTERVAL).append(str);
            if (index < size - 1){
                builder.append(",");
            }
            index++;
        }
        builder.append("\n\r)");
        builder.append("\n\r").append("VALUES (");

        index = 0;
        for (int i = 0; i < size; i++) {
            builder.append("\n\r").append(INTERVAL).append("?");
            if (index < size - 1){
                builder.append(",");
            }
            index++;
        }
        builder.append("\n\r)");
        System.out.println(builder);
    }

    private static void genUpdate(List<String> columnList,String tableName){
        StringBuilder builder = new StringBuilder("UPDATE ").append(tableName);
        builder.append("\n\r").append("SET ");
        int size = columnList.size();
        int index = 0;
        for (String str : columnList) {
            if (str.equals("id")){
                continue;
            }
            builder.append("\n\r").append(INTERVAL).append(str).append(" = ?");
            if (index < size - 2){
                builder.append(",");
            }
            index++;
        }
        builder.append("\n\r").append("WHERE id = ?");
        System.out.println(builder);
    }

    private static void genSelectById(List<String> columnList,String tableName){
        List<String> cols = new ArrayList<>();
        StringBuilder colBuilder = new StringBuilder("SELECT");
        int index = 0;
        int size = columnList.size();
        for (String str : columnList) {
            int i = str.indexOf("_");
            if (i == -1){
                colBuilder.append(INTERVAL).append(str);
            }else{
                colBuilder.append(INTERVAL).append(str).append(INTERVAL).append(transStr(str));
            }
            if (index < size - 1){
                colBuilder.append(",");
            }
            index++;
        }
        colBuilder.append("\n\r").append("FROM ").append(tableName);
        colBuilder.append("\n\r").append("WHERE id = ? ");
        System.out.println(colBuilder);
    }

    private static void genDynamicSql(Map<String,String> columnMap,String tableName){

    }

    public static String transStr(String str){
        if (StringUtils.isEmpty(str)){
            return str;
        }
        String[] arr = str.split("_");
        int len = arr.length;
        StringBuilder builder = new StringBuilder(arr[0]);
        for (int i = 1; i < len ; i++) {
            String s = arr[i];
            s = s.substring(0,1).toUpperCase() + s.substring(1);
            builder.append(s);
        }
        return builder.toString();
    }
}
